package com.douqu.game;

import com.douqu.game.core.factory.PlatformFactory;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.COSObjectSummary;
import com.qcloud.cos.model.ListObjectsRequest;
import com.qcloud.cos.model.ObjectListing;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.transfer.MultipleFileUpload;
import com.qcloud.cos.transfer.TransferManager;

import java.io.File;
import java.util.List;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2018-01-28 20:44
 */
public class CosUtils {

    public static void showDirectory()
    {
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
        listObjectsRequest.setBucketName(PlatformFactory.TENCENT_COS_BUCKET);
        listObjectsRequest.setPrefix("ServerConfig/");

        COSClient cosClient = getCOSClient();
        ObjectListing listing = cosClient.listObjects(listObjectsRequest);
        List<COSObjectSummary> list = listing.getObjectSummaries();
        MenuManager.println("当前资源服务器目录 ------> ");
        for(COSObjectSummary obj : list)
        {
            MenuManager.println("--->" + obj.getKey());
        }

        cosClient.shutdown();
    }

    public static boolean uploadFile(String remoteDirectory, String localDirectory)
    {
        File file = new File(localDirectory);
        if(!file.exists() || !file.isDirectory())
            return false;

//        File[] files = file.listFiles();
//        PutObjectRequest putObjectRequest = null;
        COSClient cosClient = getCOSClient();

        TransferManager transferManager = new TransferManager(cosClient);
        MultipleFileUpload t = transferManager.uploadDirectory(PlatformFactory.TENCENT_COS_BUCKET, remoteDirectory, file, false);
        MenuManager.println("上传文件成功---------->" + localDirectory);
        cosClient.shutdown();

//        for(File f : files)
//        {
//            if(f.getName().indexOf(".") != -1)
//                continue;
//
//            LogUtils.debug("ConfigFactory.TENCENT_COS_PREFIX -> " + ConfigFactory.TENCENT_COS_PREFIX);
//            LogUtils.debug("remoteDirectory -> " + remoteDirectory);
//            LogUtils.debug("file -> " + f.getName());
//            putObjectRequest = new PutObjectRequest(ConfigFactory.TENCENT_COS_BUCKET, ConfigFactory.TENCENT_COS_PREFIX + remoteDirectory + "/" + f.getName(), f.getAbsolutePath());
//            cosClient.putObject(putObjectRequest);
//
//            LogUtils.info("上传文件成功 ---------> " + f.getName());
//            MainFrame.println("上传文件成功 ---------> " + f.getName());
//        }
//
//        cosClient.shutdown();

        return true;
    }



    public static COSClient getCOSClient()
    {
        COSCredentials cred = new BasicCOSCredentials(PlatformFactory.TENCENT_COS_SECRET_ID, PlatformFactory.TENCENT_COS_SECRET_KEY);
        ClientConfig clientConfig = new ClientConfig(new Region(PlatformFactory.TENCENT_COS_REGION));
        return new COSClient(cred, clientConfig);
    }


//    public static void main(String[] args) {
//        LoadFactory.loadConfig();
//
//        COSClient cosClient = getCOSClient();
//        TransferManager transferManager = new TransferManager(cosClient);
//        MultipleFileDownload download = transferManager.downloadDirectory(PlatformFactory.TENCENT_COS_BUCKET, "ServerConfig/1.0.0", new File("D:/data"));
//        download.addProgressListener(new ProgressListener() {
//            @Override
//            public void progressChanged(ProgressEvent progressEvent) {
//                LogUtils.debug(progressEvent.getBytes());
//            }
//        });
//        cosClient.shutdown();
//    }
}
